{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "### 59. Spiral Matrix II\n",
    "\n",
    "Given a positive integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.\n",
    "\n",
    "    Example:\n",
    "        Input: 3\n",
    "        Output:\n",
    "        [\n",
    "         [ 1, 2, 3 ],\n",
    "         [ 8, 9, 4 ],\n",
    "         [ 7, 6, 5 ]\n",
    "        ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def generateMatrix(self, n: int) -> List[List[int]]:\n",
    "        matrix = [[0 for i in range(n)] for j in range(n)]\n",
    "        rows = columns = n - 1\n",
    "        ms = ns = 0\n",
    "        flag = 1\n",
    "        while ms <= rows and ns <= columns:\n",
    "            if ms == rows and ns == columns:\n",
    "                matrix[ms][ns] = flag\n",
    "                return matrix\n",
    "            elif ns < columns and ms == rows:\n",
    "                for j in range(ns, columns + 1, 1):\n",
    "                    matrix[ms][j] = flag\n",
    "                    flag += 1\n",
    "            elif ns == columns and ms < rows:\n",
    "                for i in range(ms, rows + 1):\n",
    "                    matrix[i][columns] = flag\n",
    "                    flag += 1\n",
    "            else:\n",
    "                for j in range(ns, columns, 1):\n",
    "                    matrix[ms][j] = flag\n",
    "                    flag += 1\n",
    "                for i in range(ms, rows):\n",
    "                    matrix[i][columns] = flag\n",
    "                    flag += 1\n",
    "                for j in range(columns, ns, -1):\n",
    "                    matrix[rows][j] = flag\n",
    "                    flag += 1\n",
    "                for i in range(rows, ms, -1):\n",
    "                    matrix[i][ns] = flag\n",
    "                    flag += 1\n",
    "            ms += 1\n",
    "            ns += 1\n",
    "            columns -= 1\n",
    "            rows -= 1\n",
    "        return matrix\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [8, 9, 4], [7, 6, 5]]\n"
     ]
    }
   ],
   "source": [
    "S = Solution()\n",
    "n = 3\n",
    "print(S.generateMatrix(n))\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}